package pers.cz.server;

import org.junit.Test;
import pers.cz.util.FileUtils;

import java.io.File;

/**
 * @program: JefLogTail
 * @description:
 * @author: Cheng Zhi
 * @create: 2023-03-05 15:28
 **/
public class LogWriteByThread {

        /**
         * @author Chengzhi
         * @date 2023-03-05
         * @测试目的： 多线程修改文件
         * @预期结果: 多个线程高并发修改文件，服务端依然可以快速处理，任务不积压
         */
        @Test
        public void testWriteFileByThread() {

            for (int i=0; i<10; i++) {
                final int j = i;
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int k=0; k<100; k++) {
                            System.out.println("写入");
                            if (k % 2 == 0) {
                                try {
                                    Thread.sleep(1000L);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            String content = Thread.currentThread().getName() + "--" + j;
                            FileUtils.writeFileByStreamByRandomAccess("D:\\test\\test1\\czzz.sql",content);
                        }
                    }
                });
                thread.start();
            }

            try {
                Thread.sleep(1000000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


}
